package oa.hleast.xswl.controller.app.pms;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import oa.hleast.common.result.Result;
import oa.hleast.xswl.pojo.bo.app.AppProductBO;
import oa.hleast.xswl.pojo.domain.pms.PmsSpu;
import oa.hleast.xswl.pojo.vo.pms.ShowPmsSpuVo;
import oa.hleast.xswl.pojo.vo.pms.ShowSpuVo;
import oa.hleast.xswl.service.pms.IAppPmsSpuService;
import oa.hleast.xswl.service.pms.IPmsSpuService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "【移动端】商品信息")
@RestController("AppProductController")
@RequestMapping("/api.app/v1/products")
@AllArgsConstructor
public class SpuController {

    private IPmsSpuService iPmsSpuService;
    private IAppPmsSpuService iAppPmsSpuService;

    @ApiOperation(value = "基础商品列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "categoryId", value = "商品类目", paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping("/baseList")
    public Result<List<ShowPmsSpuVo>> baseList(
            Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer limit,
            Long categoryId
    ) {
        IPage<ShowPmsSpuVo> result = iAppPmsSpuService.baseList(page, limit, categoryId);

        return Result.success(result.getRecords(), result.getTotal());
    }

    @ApiOperation(value = "列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "商品名称", paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "categoryId", value = "商品类目", paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping
    public Result list(
            Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer limit,
            String name,
            Long categoryId
    ) {
        IPage<PmsSpu> result = iPmsSpuService.inStockList(
                new Page<>(page, limit),
                new PmsSpu().setName(name).setCategoryId(categoryId)
        );
        return Result.success(result.getRecords(), result.getTotal());
    }


    @ApiOperation(value = "某店铺的商品列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "storeId", value = "商店ID", paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping("/spuListByStore")
    public Result spuListByStore(
            Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer limit,
            Long storeId
    ) {
        IPage<PmsSpu> result = iPmsSpuService.spuListByStore(page, limit, storeId);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @ApiOperation(value = "商品详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "商品id", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class)
    @GetMapping("/{id}")
    public Result<AppProductBO> detail(@PathVariable("id") Long id) {
        AppProductBO product = iPmsSpuService.getProductByIdForApp(id);
        return Result.success(product);
    }


    @ApiOperation(value = "热门推荐列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class)
    })
    @GetMapping("/recommend")
    public Result<List<ShowSpuVo>> recommendList(
            @RequestParam Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer limit
    ) {

        Page<ShowSpuVo> showSpuVoPage = iPmsSpuService.recommendList(page, limit);

        // TODO 推荐逻辑先按这个来，后面再迭代
//        IPage<PmsSpu> result = iPmsSpuService.page(new Page<>(page, limit), new LambdaQueryWrapper<PmsSpu>().eq(PmsSpu::getStatus, 1)
//                .orderByDesc(PmsSpu::getGmtCreate)
//        );
//
//        List<ShowSpuVo> showSpuVoList = result.getRecords().stream().map(pojo -> {
//            ShowSpuVo showSpuVo = new ShowSpuVo();
//            BeanUtil.copyProperties(pojo, showSpuVo);
//            return showSpuVo;
//        }).collect(Collectors.toList());

        return Result.success(showSpuVoPage.getRecords(), showSpuVoPage.getTotal());
    }

    @ApiOperation(value = "新增销量", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "num", value = "数量", paramType = "path", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @PostMapping("/addSales/{id}")
    public Result addSales(@PathVariable Long id, Integer num) {
        LambdaUpdateWrapper<PmsSpu> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.setSql("sales=sales+" + num);
        lambdaUpdateWrapper.eq(PmsSpu::getId, id);
        boolean status = iPmsSpuService.update(lambdaUpdateWrapper);
        return Result.judge(status);
    }
}
